package com.example.demo.algorithm;

public class PxAlgorithm {

    public static void main(String[] args) {
        xz();
    }

    /**
     * 冒泡排序
     */
    private static void mp() {
        int[] number = {2, 4, 1, 9, 10, 7, 6, 3, 0};
        for (int i = 0; i < number.length - 1; i++) {
            for (int j = 0; j < number.length - i - 1; j++) {
                if (number[j] > number[j + 1]) {
                    int max = number[j]; // 保存大的值
                    number[j] = number[j + 1];
                    number[j + 1] = max;
                }
            }
        }
        for (int a : number) {
            System.out.println(String.valueOf(a));
        }
    }

    /**
     * 选择排序
     */
    private static void xz() {
        int[] number = {2, 4, 1, 9, 10, 7, 6, 3, 0};
        for (int i = 0; i < number.length; i++) {
            int index = i; // 假设最小值的下标
            int min = number[index]; // 假设最小值
            for (int j = i+1; j < number.length; j++) {
                if (min > number[j]){ //如果比最小值还小的
                    index = j; // 记录比最小值还小的下标
                    min = number[j]; // 记录比最小值还小的值
                }
            }
            number[index] = number[i];  // 第一次 {2, 4, 1, 9, 10, 7, 6, 3, 2};
            number[i] = min; // 最小值
        }
        for (int s: number) {
            System.out.println(String.valueOf(s));
        }
    }
}
